package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.McgsQk1QkfzBo;
import com.ruoyi.tb.lmt.domain.vo.McgsQk1QkfzVo;
import com.ruoyi.tb.lmt.domain.McgsQk1Qkfz;
import com.ruoyi.tb.lmt.mapper.McgsQk1QkfzMapper;
import com.ruoyi.tb.lmt.service.IMcgsQk1QkfzService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 制动阀试验台_QK1气控阀组_MCGSService业务层处理
 *
 * @author ruoyi
 * @date 2023-12-13
 */
@RequiredArgsConstructor
@Service
public class McgsQk1QkfzServiceImpl implements IMcgsQk1QkfzService {

    private final McgsQk1QkfzMapper baseMapper;

    /**
     * 查询制动阀试验台_QK1气控阀组_MCGS
     */
    @Override
    public McgsQk1QkfzVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询制动阀试验台_QK1气控阀组_MCGS列表
     */
    @Override
    public TableDataInfo<McgsQk1QkfzVo> queryPageList(McgsQk1QkfzBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<McgsQk1Qkfz> lqw = buildQueryWrapper(bo);
        Page<McgsQk1QkfzVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询制动阀试验台_QK1气控阀组_MCGS列表
     */
    @Override
    public List<McgsQk1QkfzVo> queryList(McgsQk1QkfzBo bo) {
        LambdaQueryWrapper<McgsQk1Qkfz> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<McgsQk1Qkfz> buildQueryWrapper(McgsQk1QkfzBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<McgsQk1Qkfz> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, McgsQk1Qkfz::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getDjNo()), McgsQk1Qkfz::getDjNo, bo.getDjNo());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTime()), McgsQk1Qkfz::getMcgsTime, bo.getMcgsTime());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTimeMs()), McgsQk1Qkfz::getMcgsTimeMs, bo.getMcgsTimeMs());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsCzz()), McgsQk1Qkfz::getMcgsCzz, bo.getMcgsCzz());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJxbh()), McgsQk1Qkfz::getMcgsJxbh, bo.getMcgsJxbh());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsFh()), McgsQk1Qkfz::getMcgsFh, bo.getMcgsFh());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsSch()), McgsQk1Qkfz::getMcgsSch, bo.getMcgsSch());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsBy1()), McgsQk1Qkfz::getMcgsBy1, bo.getMcgsBy1());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsZsch()), McgsQk1Qkfz::getMcgsZsch, bo.getMcgsZsch());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsZdf4()), McgsQk1Qkfz::getMcgsZdf4, bo.getMcgsZdf4());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJxbhsr()), McgsQk1Qkfz::getMcgsJxbhsr, bo.getMcgsJxbhsr());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsZdf1()), McgsQk1Qkfz::getMcgsZdf1, bo.getMcgsZdf1());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsZdf2()), McgsQk1Qkfz::getMcgsZdf2, bo.getMcgsZdf2());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsZdf3()), McgsQk1Qkfz::getMcgsZdf3, bo.getMcgsZdf3());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), McgsQk1Qkfz::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), McgsQk1Qkfz::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, McgsQk1Qkfz::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), McgsQk1Qkfz::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, McgsQk1Qkfz::getUpdateTime, bo.getUpdateTime());
        return lqw;
    }

    /**
     * 新增制动阀试验台_QK1气控阀组_MCGS
     */
    @Override
    public Boolean insertByBo(McgsQk1QkfzBo bo) {
        McgsQk1Qkfz add = BeanUtil.toBean(bo, McgsQk1Qkfz.class);
        add.setDelFlag("0");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改制动阀试验台_QK1气控阀组_MCGS
     */
    @Override
    public Boolean updateByBo(McgsQk1QkfzBo bo) {
        McgsQk1Qkfz update = BeanUtil.toBean(bo, McgsQk1Qkfz.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(McgsQk1Qkfz entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除制动阀试验台_QK1气控阀组_MCGS
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public String getMaxMcgsTime(){
        return baseMapper.getMaxMcgsTime();
    }
}
